package com.cwits.wifi.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


import com.cwits.wifi.data.beans.VideoFileInfo;
import com.cwits.wifi.util.TimeFormater;
import com.jieli.lib.stream.beans.VideoInfo;

import java.util.Calendar;
import java.util.List;

/**
 * Created by zhanghua on 2016/2/23.
 */
public class TimeAxisView extends View{
    String TAG = "---TimeAxisView----------";
    private Paint mPaint;
    private Canvas mCanvas;
    private List<VideoInfo> mVideoFileInfoList;
    int Color_default = 0x1000;//颜色
    private int mCount = 0;//需要绘制的块数
    private int mCurrentGrade = 1;
    private float mCurrentScale = 0;
    private long mCurrentTime = 0L;
    private long mMinVideoTime = 0L;
    private long mMaxVideoTime = 0L;

    private int mModeDragOrZoom = 0; //1为拖拽，2为缩放
    private float mDragDistance = 0;

    private float mDistanceForTwoFinger = 0;
    private float mCurrnetX = 0;

    /**
     * 监听
     */
    public interface OnTimeAxisInfoListener{
        void OnShowTimeAxisInfo(String info);
        void OnMiddleTimeAxisInfo(String info,long time);
    }

    private OnTimeAxisInfoListener mShowInfo_Listener = null;

    /**
     * 构造
     * @param context
     */
    public TimeAxisView(Context context) {
        super(context);
        init();
    }

    /**
     * 构造
     * @param context
     * @param attrs
     */
    public TimeAxisView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    /**
     * 构造
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public TimeAxisView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //初始化
    private void init()
    {
        mPaint = new Paint();
        mCurrentTime = System.currentTimeMillis();
        mMinVideoTime = mCurrentTime - 12*60*60*1000;
        mMaxVideoTime = mCurrentTime + 12*60*60*1000;
        mCurrentGrade = 5;
        mModeDragOrZoom = -1;
    }

    /**
     * 设置监听，输出信息
     * @param showinfo_listener
     */
    public void setOnTimeAxisInfo(OnTimeAxisInfoListener showinfo_listener)
    {
        mShowInfo_Listener = showinfo_listener;
    }

    /**
     * 设置视频列表
     * @param videofile_list
     */
    public void setVideoFileList(List<VideoInfo> videofile_list)
    {
        mVideoFileInfoList = videofile_list;
        mCurrentTime = System.currentTimeMillis();
        long time = mMaxVideoTime - mMinVideoTime;
        if(mVideoFileInfoList != null&&mVideoFileInfoList.size() == 1)
        {
            mMinVideoTime = TimeFormater.stringTolong(mVideoFileInfoList.get(0).getCreateDate(),TimeFormater.format1) - 48*24*60*60*1000;
            mMaxVideoTime = TimeFormater.stringTolong(mVideoFileInfoList.get(0).getCreateDate(),TimeFormater.format1) + 48*24*60*60*1000 +  mVideoFileInfoList.get(mVideoFileInfoList.size() - 1).getDuration()*1000;
            mCurrentTime = TimeFormater.stringTolong(mVideoFileInfoList.get(0).getCreateDate(),TimeFormater.format1) +  mVideoFileInfoList.get(mVideoFileInfoList.size() - 1).getDuration()*1000;;
        }else if(mVideoFileInfoList != null &&mVideoFileInfoList.size() > 1)
        {
            mMinVideoTime = TimeFormater.stringTolong(mVideoFileInfoList.get(mVideoFileInfoList.size() - 1).getCreateDate(),TimeFormater.format1) - 48*24*60*60*1000;
            mMaxVideoTime = TimeFormater.stringTolong(mVideoFileInfoList.get(0).getCreateDate(),TimeFormater.format1)
                    + mVideoFileInfoList.get(0).getDuration()*1000 + 48*24*60*60*1000;
            mCurrentTime = TimeFormater.stringTolong(mVideoFileInfoList.get(0).getCreateDate(),TimeFormater.format1)
                    + mVideoFileInfoList.get(0).getDuration()*1000;
        }

        mCurrentGrade = 5;
        mModeDragOrZoom = -1;
    }

    /**
     * 缩小比例
     * @param current_grade
     * @return
     */
    private int reduce(int current_grade)
    {
        int grade = 1;
        if(current_grade > 1)
        {
            grade = current_grade - 1;
        }else
        {
            grade = current_grade;
        }
        return grade;
    }

    /**
     * 放大比例
     * @param current_grade
     * @return
     */
    private int enLarger(int current_grade)
    {
        int grade = 10;
        if(current_grade < 10)
        {
            grade = current_grade + 1;
        }else
        {
            grade = current_grade;
        }
        return grade;
    }

    /**
     * 重新设置宽高
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height ;
        if (widthMode == MeasureSpec.EXACTLY)
        {
            width = widthSize;
        } else
        {
            width = widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY)
        {
            height = heightSize;
        } else
        {
            height = heightSize;
        }

        setMeasuredDimension(width, height);
    }

    /**
     * 绘制
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();

        //拖拽或是缩放处理
        if(mModeDragOrZoom == 1)
        {
            //拖拽
            if(mDragDistance > 0)
            {
                //向右拖拽
                int time = (int)(mCurrentScale * mDragDistance);
                mCurrentTime = mCurrentTime - time;
                if(mCurrentTime < mMinVideoTime)
                    mCurrentTime = mMinVideoTime;

            }else
            {
                //向左拖拽
                int time = (int)(mCurrentScale * (-mDragDistance));
                mCurrentTime = mCurrentTime + time;
                if(mCurrentTime > mMaxVideoTime)
                    mCurrentTime = mMaxVideoTime;

            }

            drawRec(canvas,width, height);
            drawScale(canvas, width, height);
            drawVideoRec(canvas,width,height);


        }else if(mModeDragOrZoom == 2)
        {
            //获取当前的比例尺
            mCurrentScale = getScaleByGrade(mCurrentGrade,width);
            drawRec(canvas,width, height);
            drawScale(canvas, width, height);
            drawVideoRec(canvas,width,height);


        }else {
            mCurrentScale = getScaleByGrade(mCurrentGrade,width);
            drawRec(canvas,width, height);
            drawScale(canvas, width, height);
            drawVideoRec(canvas,width,height);
        }

    }

    /**
     * 主动去绘制，在回放模式下，根据视频播放的信息更新进度
     */
    public void draw()
    {
        this.invalidate();
    }

    /**
     * 主动去绘制，在回放模式下，根据视频播放的信息更新进度
     */
    public void draw(int time_en)
    {
        this.invalidate();
    }

    /**
     * 绘制整体区域
     * @param canvas
     * @param width
     * @param height
     */
    private void drawRec(Canvas canvas,int width,int height)
    {
        mPaint.setStyle(Paint.Style.FILL); //设置填充
        mPaint.setColor(Color.BLACK);
        canvas.drawRect(0, 0, width, height, mPaint);
    }

    /**
     * 绘制视频可视区域
     * @param canvas
     * @param width
     * @param height
     */
    private void drawVideoRec(Canvas canvas,int width,int height)
    {
        //mCurrentTime;
        //mCurrentScale
        mPaint.setColor(Color.RED);
        if(mVideoFileInfoList != null&&mVideoFileInfoList.size() > 0)
        {
            for(int i = 0;i < mVideoFileInfoList.size();i++)
            {
                long start_time = TimeFormater.stringTolong(mVideoFileInfoList.get(i).getCreateDate(),TimeFormater.format1);
                long stop_time = start_time + mVideoFileInfoList.get(i).getDuration()*1000;
                if(mCurrentTime - start_time > 0 &&mCurrentTime - stop_time >= 0)
                {
                    float interval_stop  = ((mCurrentTime - stop_time)/mCurrentScale);
                    if(interval_stop >= width/2)
                        continue;
                    float interval_start  = ((mCurrentTime - start_time)/mCurrentScale);
                    if(mVideoFileInfoList.get(i).getIsLocked())
                        mPaint.setColor(Color.GREEN);
                    canvas.drawRect(width/2 - interval_start,0, width/2 - interval_stop, height, mPaint); //绘制矩形
                }else if(mCurrentTime - start_time <= 0 &&mCurrentTime - stop_time < 0)
                {
                    float interval_start  = ((start_time - mCurrentTime)/mCurrentScale);
                    if(interval_start >= width/2)
                        continue;
                    float interval_stop  = ((stop_time - mCurrentTime)/mCurrentScale);
                    if(mVideoFileInfoList.get(i).getIsLocked())
                        mPaint.setColor(Color.GREEN);
                    canvas.drawRect(width/2 + interval_start,0, width/2 + interval_stop, height, mPaint); //绘制矩形
                }else if(mCurrentTime - start_time > 0 &&mCurrentTime - stop_time < 0)
                {
                    float interval_start  = ((mCurrentTime - start_time)/mCurrentScale);
                    float interval_stop  = ((stop_time - mCurrentTime)/mCurrentScale);
                    if(mVideoFileInfoList.get(i).getIsLocked())
                        mPaint.setColor(Color.GREEN);
                    canvas.drawRect(width/2 - interval_start,0, width/2 + interval_stop, height, mPaint); //绘制矩形
                }

            }
        }

    }

    /**
     * 绘制刻度尺
     * @param canvas
     * @param width
     * @param height
     */
    private void drawScale(Canvas canvas,int width,int height)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(mCurrentTime);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int sec = calendar.get(Calendar.SECOND);

        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(16);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); //消除锯齿
        calendar.set(Calendar.HOUR_OF_DAY, hour - 1);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);


        switch(mCurrentGrade)
        {
            case 1:
                //间隔24h
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 2:
                //间隔12h
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 3:
                //间隔6h
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 4:
                //间隔3h
                calendar.set(Calendar.HOUR_OF_DAY, hour - 1);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 5:
                //间隔1h
                calendar.set(Calendar.HOUR_OF_DAY, hour - 1);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 6:
                //间隔30min
                calendar.set(Calendar.HOUR_OF_DAY, hour - 1);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 7:
                //间隔10min
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 8:
                //间隔5min
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                break;
            case 9:
                //间隔1min
                calendar.set(Calendar.SECOND,0);
                break;
            case 10:
                //间隔30s
                calendar.set(Calendar.SECOND,0);
                break;
        }

        long timescale = calendar.getTimeInMillis();
        int scale = width/2 - (int)((mCurrentTime - timescale)/mCurrentScale);
        drawLineAndText(canvas,scale,calendar,width,height);

    }

    private void drawLineAndText(Canvas canvas,int current_width,Calendar calendar,int width,int height)
    {
        String text  = "";
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        switch(mCurrentGrade)
        {
            case 1:
                //间隔24h
                //右边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 24*60*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    String month_text = "";
                    String day_text = "";
                    if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12)
                    {
                        //每个月31天
                        day_text = String.valueOf(day + i);
                        month_text = String.valueOf(month);
                        if(day + i >31)
                        {
                            day_text = String.valueOf(day + i - 31);
                            if(month == 12)
                            {
                                month_text = "1";
                            }else
                            {
                                month_text = String.valueOf(month + 1);
                            }


                        }
                    }else if(month == 2)
                    {
                        //判断是否是闰年
                        if(year%4 == 0)
                        {
                            day_text = String.valueOf(day + i);
                            month_text = String.valueOf(month);
                            if(day + i > 29)
                            {
                                day_text = String.valueOf(day + i - 29);
                                month_text = String.valueOf(month + 1);

                            }
                        }else
                        {
                            day_text = String.valueOf(day + i);
                            month_text = String.valueOf(month);
                            if(day + i > 28)
                            {
                                day_text = String.valueOf(day + i - 28);
                                month_text = String.valueOf(month + 1);

                            }
                        }
                    }else
                    {
                        //每个月30天
                        day_text = String.valueOf(day + i);
                        month_text = String.valueOf(month);
                        if(day + i >30)
                        {
                            day_text = String.valueOf(day + i - 30);
                            month_text = String.valueOf(month + 1);


                        }

                    }
                    text = month_text + "月" + day_text + "日";
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (24*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (24*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 24*60*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    mPaint.setColor(Color.WHITE);
                    String month_text = "";
                    String day_text = "";
                    if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12)
                    {
                        //每个月31天
                        day_text = String.valueOf(day - i);
                        month_text = String.valueOf(month);
                        if(day - i < 0)
                        {
                            if (month == 8)
                            {
                                day_text = String.valueOf(day - i + 31);
                            }else if(month == 3)
                            {
                                if(year%4 == 0)
                                {
                                    day_text = String.valueOf(day - i + 29);
                                }else
                                {
                                    day_text = String.valueOf(day - i + 28);
                                }

                            }else
                            {
                                day_text = String.valueOf(day - i + 30);
                            }

                            if(month == 1)
                            {
                                month_text = "12";
                            }else
                            {
                                month_text = String.valueOf(month - 1);
                            }


                        }
                    }else
                    {
                        //每个月30天
                        day_text = String.valueOf(day - i);
                        month_text = String.valueOf(month);
                        if(day - i < 0)
                        {
                            day_text = String.valueOf(day - i + 31);
                            month_text = String.valueOf(month - 1);

                        }

                    }
                    text = month_text + "月" + day_text + "日";
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (24*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (24*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 2:
                //间隔12h
                //右边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 12*60*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    String month_text = "";
                    String day_text = "";
                    int day_interval = i/2;
                    int modes = i%2;
                    if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12)
                    {
                        //每个月31天
                        day_text = String.valueOf(day + day_interval);
                        month_text = String.valueOf(month);
                        if(day + day_interval >31)
                        {
                            day_text = String.valueOf(day + day_interval - 31);
                            if(month == 12)
                            {
                                month_text = "1";
                            }else
                            {
                                month_text = String.valueOf(month + 1);
                            }
                        }
                    }else if(month == 2)
                    {
                        //判断是否是闰年
                        if(year%4 == 0)
                        {
                            day_text = String.valueOf(day + day_interval);
                            month_text = String.valueOf(month);
                            if(day + day_interval > 29)
                            {
                                day_text = String.valueOf(day + day_interval - 29);
                                month_text = String.valueOf(month + 1);

                            }
                        }else
                        {
                            day_text = String.valueOf(day + day_interval);
                            month_text = String.valueOf(month);
                            if(day + day_interval > 28)
                            {
                                day_text = String.valueOf(day + day_interval - 28);
                                month_text = String.valueOf(month + 1);

                            }
                        }
                    }else
                    {
                        //每个月30天
                        day_text = String.valueOf(day + day_interval);
                        month_text = String.valueOf(month);
                        if(day + day_interval >30)
                        {
                            day_text = String.valueOf(day + day_interval - 30);
                            month_text = String.valueOf(month + 1);


                        }
                    }
                    if(modes == 0)
                    {
                        text = month_text + "月" + day_text + "日" + "00时";
                    }else
                    {
                        text = month_text + "月" + day_text + "日" + "12时";
                    }

                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (12*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (12*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 12*60*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    mPaint.setColor(Color.WHITE);
                    String month_text = "";
                    String day_text = "";
                    int day_interval = i/2;
                    int modes = i%2;
                    if(modes == 1)
                        day_interval =day_interval + 1;
                    if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12)
                    {
                        //每个月31天
                        day_text = String.valueOf(day - day_interval);
                        month_text = String.valueOf(month);
                        if(day - day_interval < 0)
                        {
                            if (month == 8)
                            {
                                day_text = String.valueOf(day - day_interval + 31);
                            }else if(month == 3)
                            {
                                if(year%4 == 0)
                                {
                                    day_text = String.valueOf(day - day_interval + 29);
                                }else
                                {
                                    day_text = String.valueOf(day - day_interval + 28);
                                }

                            }else
                            {
                                day_text = String.valueOf(day - day_interval + 30);
                            }

                            if(month == 1)
                            {
                                month_text = "12";
                            }else
                            {
                                month_text = String.valueOf(month - 1);
                            }


                        }
                    }else
                    {
                        //每个月30天
                        day_text = String.valueOf(day - day_interval);
                        month_text = String.valueOf(month);
                        if(day - day_interval < 0)
                        {
                            day_text = String.valueOf(day - day_interval + 31);
                            month_text = String.valueOf(month - 1);


                        }

                    }
                    if(modes == 0)
                    {
                        text = month_text + "月" + day_text + "日" + "0时";
                    }else
                    {
                        text = month_text + "月" + day_text + "日" + "12时";
                    }

                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (12*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (12*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 3:
                //间隔6h
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 6*60*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + 6*i;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    text = String.valueOf(hour_target) + ":00";
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (6*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (6*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 6*60*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - 6*i;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;
                    text = String.valueOf(hour_target) + ":00";

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (6*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (6*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 4:
                //间隔3h
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 3*60*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + 3*i;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    text = String.valueOf(hour_target) + ":00";
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (3*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (3*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 3*60*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - 3*i;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;
                    text = String.valueOf(hour_target) + ":00";

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (3*60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (3*60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 5:
                //间隔1h
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 60*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + i;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    text = String.valueOf(hour_target) + ":00";
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 60*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - i;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;
                    text = String.valueOf(hour_target) + ":00";

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (60*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (60*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 6:
                //间隔30min
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 30*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    int hour_interval = i/2;
                    int modes = i%2;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + hour_interval;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    if(modes == 0)
                    {
                        text = String.valueOf(hour_target) + ":00";
                    }else
                    {
                        text = String.valueOf(hour_target) + ":30";
                    }
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (30*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (30*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 30*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    int hour_interval = i/2;
                    int modes = i%2;
                    if(modes == 1)
                        hour_interval = hour_interval + 1;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - hour_interval;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;

                    if(modes == 0)
                    {
                        text = String.valueOf(hour_target) + ":00";
                    }else
                    {
                        text = String.valueOf(hour_target) + ":30";
                    }

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (30*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (30*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 7:
                //间隔10min
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 10*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    int hour_interval = i/6;
                    int modes = i%6;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + hour_interval;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    int min_target = min;
                    switch(modes)
                    {
                        case 1:
                            min_target = min + 10;
                            break;
                        case 2:
                            min_target = min + 20;
                            break;
                        case 3:
                            min_target = min + 30;
                            break;
                        case 4:
                            min_target = min + 40;
                            break;
                        case 5:
                            min_target = min + 50;
                            break;
                    }
                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (10*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (10*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 10*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    int hour_interval = i/6;
                    int modes = i%6;
                    int min_target = 00;
                    switch(modes)
                    {
                        case 1:
                            min_target = 50;
                            break;
                        case 2:
                            min_target = 40;
                            break;
                        case 3:
                            min_target = 30;
                            break;
                        case 4:
                            min_target = 20;
                            break;
                        case 5:
                            min_target = 10;
                            break;
                    }

                    if(modes != 0)
                        hour_interval = hour_interval + 1;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - hour_interval;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;

                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (10*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (10*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 8:
                //间隔5min
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 5*60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    int hour_interval = i/12;
                    int modes = i%12;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + hour_interval;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    int min_target = min;
                    switch(modes)
                    {
                        case 1:
                            min_target = min + 05;
                            break;
                        case 2:
                            min_target = min + 10;
                            break;
                        case 3:
                            min_target = min + 15;
                            break;
                        case 4:
                            min_target = min + 20;
                            break;
                        case 5:
                            min_target = min + 25;
                            break;
                        case 6:
                            min_target = min + 30;
                            break;
                        case 7:
                            min_target = min + 35;
                            break;
                        case 8:
                            min_target = min + 40;
                            break;
                        case 9:
                            min_target = min + 45;
                            break;
                        case 10:
                            min_target = min + 50;
                            break;
                        case 11:
                            min_target = min + 55;
                            break;
                    }
                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (5*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (5*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 5*60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    int hour_interval = i/12;
                    int modes = i%12;
                    int min_target = 00;
                    switch(modes)
                    {
                        case 1:
                            min_target = 55;
                            break;
                        case 2:
                            min_target = 50;
                            break;
                        case 3:
                            min_target = 45;
                            break;
                        case 4:
                            min_target = 40;
                            break;
                        case 5:
                            min_target = 35;
                            break;
                        case 6:
                            min_target = 30;
                            break;
                        case 7:
                            min_target = 25;
                            break;
                        case 8:
                            min_target = 20;
                            break;
                        case 9:
                            min_target = 15;
                            break;
                        case 10:
                            min_target = 10;
                            break;
                        case 11:
                            min_target = 05;
                            break;
                    }

                    if(modes != 0)
                        hour_interval = hour_interval + 1;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - hour_interval;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;

                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (5*60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (5*60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 9:
                //间隔1min
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 60*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    int hour_interval = i/60;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour + hour_interval;
                    if(hour_target > 24)
                        hour_target = hour_target - 24;
                    int min_target = min + i;
                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);
                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (60*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 60*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    int hour_interval = i/60;
                    int min_target = 60 - i%60;
                    if(min_target == 60)
                        min_target = 00;
                    if(i%60 != 0)
                        hour_interval = hour_interval + 1;
                    mPaint.setColor(Color.WHITE);
                    int hour_target = hour - hour_interval;
                    if(hour_target < 0)
                        hour_target = hour_target + 24;

                    text = String.valueOf(hour_target) + ":" + String.valueOf(min_target);

                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (60*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (60*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
            case 10:
                //间隔30s
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 30*1000*i/mCurrentScale;
                    if(current_width + interval > width)
                        break;
                    mPaint.setColor(Color.WHITE);
                    int min_interval = i/2;
                    int min_target = min + min_interval;
                    if(min_target >= 60)
                        min_target = min_target - 60;
                    if(i%2 == 0)
                    {
                        text = String.valueOf(min_target) + "分" + "0秒";
                    }else
                    {
                        text = String.valueOf(min_target) + "分" + "30秒";
                    }

                    mPaint.setColor(Color.WHITE);
                    canvas.drawLine(current_width + interval, 5, current_width + interval, height - 5, mPaint);
                    canvas.drawText(text, current_width + interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width + interval + (30*1000/mCurrentScale)*(j + 1)/6, 20, current_width + interval + (30*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                //左边绘制
                for(int i = 0;i < 1000;i++)
                {
                    float interval = 30*1000*i/mCurrentScale;
                    if(current_width - interval < 0)
                        break;
                    int min_interval = i/2;
                    int min_target = min - min_interval;
                    if(i%2 == 0)
                        min_target = min_target - 1;
                    if(min_target < 0)
                        min_target = min_target + 60;

                    if(i%2 == 0)
                    {
                        text = String.valueOf(min_target) + "分" + "00秒";
                    }else
                    {
                        text = String.valueOf(min_target) + "分" + "30秒";
                    }


                    canvas.drawLine(current_width - interval, 5, current_width - interval, height - 5, mPaint);
                    canvas.drawText(text, current_width - interval, height, mPaint);
                    for(int j = 0;j < 5; j++ )
                    {
                        canvas.drawLine(current_width - interval - (30*1000/mCurrentScale)*(j + 1)/6, 20, current_width - interval - (30*1000/mCurrentScale)*(j + 1)/6, height - 20, mPaint);
                    }

                }
                break;
        }
    }

    /**
     * 通过等级获取刻度尺
     * @param grade
     * @return
     */
    private float getScaleByGrade(int grade,int width)
    {
        float scale = 24;
        //if(mVideoFileInfoList == null ||mVideoFileInfoList.size() == 0)
         //   return (mMaxVideoTime - mMinVideoTime)/(12*width);
        switch(grade)
        {
            case 1:
                //间隔24h
                scale = (float)(1000*60*60*24*2 /width);
                break;
            case 2:
                //间隔12h
                scale = (float)(1000*60*60*24 / width);
                break;
            case 3:
                //间隔6h
                scale = (float)(1000*60*60*12 / (width));
                break;
            case 4:
                //间隔3h
                scale = (float)(1000*60*60*6 / ( width));
                break;
            case 5:
                //间隔1h
                scale = (float)(1000*60*60*2 / (width));
                break;
            case 6:
                //间隔30min
                scale = (float)(1000*60*60 / (width));
                break;
            case 7:
                //间隔10min
                scale = (float)(1000*60*20 / (width));
                break;
            case 8:
                //间隔5min
                scale = (float)(1000*60*10 / (width));
                break;
            case 9:
               //间隔1min
                scale = (float)(1000*60*2 / ( width));
                break;
            case 10:
                //间隔30s
                scale = (float)(1000*60*1 / ( width));
                break;
        }
        return scale;
    }

    /**
     * 触摸监听
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean flag = false;
        switch(event.getAction()& MotionEvent.ACTION_MASK)
        {
            //一个手指按下
            case MotionEvent.ACTION_DOWN:
                mModeDragOrZoom = 1;
                mCurrnetX = event.getX();
                Log.i(TAG,"MotionEvent.ACTION_DOWN,X:" + String.valueOf(mCurrnetX));
                break;
            //移动
            case MotionEvent.ACTION_MOVE:
                if(mModeDragOrZoom == 1)
                {
                    //拖拽
                    float x = event.getX();
                    Log.i(TAG,"MotionEvent.ACTION_MOVE,--DRAG---,X:" + String.valueOf(x));
                    if(x > mCurrnetX)
                    {
                        //右拖拽
                        Log.i(TAG,"MotionEvent.ACTION_MOVE,--DRAG--,to_right");
                        if(x - mCurrnetX > 1)
                        {
                            float currnetX = x;
                            mDragDistance = x - mCurrnetX;
                            mCurrnetX = currnetX;
                            this.invalidate();
                        }

                    }else if(x < mCurrnetX)
                    {
                        //左拖拽
                        Log.i(TAG,"MotionEvent.ACTION_MOVE,--DRAG--,to_left");
                        if(mCurrnetX - x > 1)
                        {
                            float currnetX = x;
                            mDragDistance = x - mCurrnetX;
                            mCurrnetX = currnetX;
                            this.invalidate();
                        }
                    }

                }else if(mModeDragOrZoom == 2)
                {
                    //缩放
                    float distance = distance(event);
                    Log.i(TAG,"MotionEvent.ACTION_MOVE,--ZOOM--,distance:" + String.valueOf(distance));
                    if(distance > mDistanceForTwoFinger)
                    {
                        //放大
                        Log.i(TAG,"MotionEvent.ACTION_MOVE,--ZOOMOUT--,distance:");
                        if(distance - mDistanceForTwoFinger > 20)
                        {
                            mDistanceForTwoFinger = distance;
                            mCurrentGrade = enLarger(mCurrentGrade);
                            this.invalidate();
                        }

                    }else if(distance < mDistanceForTwoFinger)
                    {
                        //缩小
                        Log.i(TAG,"MotionEvent.ACTION_MOVE,--ZOOMINT--,distance:");
                        if(mDistanceForTwoFinger - distance > 20)
                        {
                            mDistanceForTwoFinger = distance;
                            mCurrentGrade = reduce(mCurrentGrade);
                            this.invalidate();
                        }
                    }
                }
                break;
            //手指从屏幕中抬起
            case MotionEvent.ACTION_UP:
                mModeDragOrZoom = 0;
                Log.i(TAG,"MotionEvent.ACTION_UP,--simpler--,distance:");
                break;
            // 当屏幕上已经有触点（手指），再有一个手指压下屏幕
            case MotionEvent.ACTION_POINTER_DOWN:
                mModeDragOrZoom = 2;
                Log.i(TAG,"MotionEvent.ACTION_POINTER_DOWN,--twofinger--,distance:");
                mDistanceForTwoFinger = distance(event);
                break;
            // 有手指离开屏幕，但屏幕还有触点（手指）
            case MotionEvent.ACTION_POINTER_UP:
                Log.i(TAG,"MotionEvent.ACTION_POINTER_UP,--up_pointer--,distance:");
                mModeDragOrZoom = 0;
                break;
            default:

                break;
        }
        return true;
    }

    /**
     * 计算两点之间的距离
     *
     * @param event
     * @return
     */
    public static float distance(MotionEvent event) {
        float dx = event.getX(1) - event.getX(0);
        float dy = event.getY(1) - event.getY(0);
        return FloatMath.sqrt(dx * dx + dy * dy);
    }
}
